<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>CSS规范 | 孙加俊的个人博客</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/sjj-blog/favicon.ico">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/sjj-blog/assets/css/0.styles.a5cb6817.css" as="style"><link rel="preload" href="/sjj-blog/assets/js/app.ab5bad9b.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/3.136f62ef.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/1.a27fba8e.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/15.b0aaec82.js" as="script"><link rel="prefetch" href="/sjj-blog/assets/js/10.9271e5b8.js"><link rel="prefetch" href="/sjj-blog/assets/js/11.67b9ea44.js"><link rel="prefetch" href="/sjj-blog/assets/js/12.f69e6353.js"><link rel="prefetch" href="/sjj-blog/assets/js/13.ace41b19.js"><link rel="prefetch" href="/sjj-blog/assets/js/14.78670b1e.js"><link rel="prefetch" href="/sjj-blog/assets/js/16.f0d479dc.js"><link rel="prefetch" href="/sjj-blog/assets/js/17.1bacbab3.js"><link rel="prefetch" href="/sjj-blog/assets/js/18.2237a65f.js"><link rel="prefetch" href="/sjj-blog/assets/js/19.7e0f4bf8.js"><link rel="prefetch" href="/sjj-blog/assets/js/20.5c098df8.js"><link rel="prefetch" href="/sjj-blog/assets/js/21.45bd5038.js"><link rel="prefetch" href="/sjj-blog/assets/js/4.cc582e42.js"><link rel="prefetch" href="/sjj-blog/assets/js/5.9c9443fe.js"><link rel="prefetch" href="/sjj-blog/assets/js/6.370fb6dd.js"><link rel="prefetch" href="/sjj-blog/assets/js/7.e5d26899.js"><link rel="prefetch" href="/sjj-blog/assets/js/8.f6b78670.js"><link rel="prefetch" href="/sjj-blog/assets/js/9.d915a2b8.js">
    <link rel="stylesheet" href="/sjj-blog/assets/css/0.styles.a5cb6817.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>孙加俊的个人博客</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2></p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>孙加俊</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/sjj-blog/" class="home-link router-link-active"><img src="/sjj-blog/logo.png" alt="孙加俊的个人博客" class="logo"> <span class="site-name">孙加俊的个人博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/sjj-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      技术栈
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/规范/" class="nav-link"><i class="undefined"></i>
  规范
</a></li><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/原生JS/" class="nav-link"><i class="undefined"></i>
  原生JS
</a></li></ul></div></div><div class="nav-item"><a href="/sjj-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/sir-jun" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/sjj-blog/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    孙加俊
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>11</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>3</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/sjj-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      技术栈
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/规范/" class="nav-link"><i class="undefined"></i>
  规范
</a></li><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/原生JS/" class="nav-link"><i class="undefined"></i>
  原生JS
</a></li></ul></div></div><div class="nav-item"><a href="/sjj-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/sir-jun" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>CSS规范</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>孙加俊</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">CSS规范</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>孙加俊</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>7/20/2023</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>规范</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="css规范"><a href="#css规范" class="header-anchor">#</a> CSS规范</h1> <h2 id="命名"><a href="#命名" class="header-anchor">#</a> 命名</h2> <ul><li>类名使用小写字母，以下划线分隔</li> <li>id 采用驼峰式命名</li> <li>scss 中的变量、函数、混合、placeholder 采用驼峰式命名</li> <li>ID 和 class 的名称总是使用可以反应元素目的和用途的名称，或其他通用的名称，代替表象和 晦涩难懂的名称</li></ul> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.heavy</span> <span class="token punctuation">{</span> <span class="token property">font-weight</span><span class="token punctuation">:</span> 800<span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token selector">.top_title</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.fw-800</span> <span class="token punctuation">{</span> <span class="token property">font-weight</span><span class="token punctuation">:</span> 800<span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token selector">.red</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span>
</code></pre></div><h2 id="选择器"><a href="#选择器" class="header-anchor">#</a> 选择器</h2> <h3 id="css-选择器中避免使用标签名"><a href="#css-选择器中避免使用标签名" class="header-anchor">#</a> css 选择器中避免使用标签名</h3> <ul><li>从结构、表现、行为分离的原则来看，应该尽量避免 css 中出现 HTML 标签，并且在 css 选择 器中出现标签名会存在潜在的问题。</li></ul> <h3 id="使用直接子选择器"><a href="#使用直接子选择器" class="header-anchor">#</a> 使用直接子选择器</h3> <ul><li>很多前端开发人员写选择器链的时候不使用 直接子选择器（注：直接子选择器和后代选择器的 区别）。有时，这可能会导致疼痛的设计问题并且有时候可能会很耗性能。然而，在任何情况下， 这是一个非常不好的做法。如果你不写很通用的，需要匹配到 DOM 末端的选择器， 你应该总 是考虑直接子选择器</li></ul> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.content &gt; .title</span> <span class="token punctuation">{</span> 
    <span class="token property">font-size</span><span class="token punctuation">:</span> 2rem<span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.content .title</span> <span class="token punctuation">{</span> 
  <span class="token property">font-size</span><span class="token punctuation">:</span> 2rem<span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre></div><h2 id="尽量使用缩写属性"><a href="#尽量使用缩写属性" class="header-anchor">#</a> 尽量使用缩写属性</h2> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token property">border-top</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> 
<span class="token property">font</span><span class="token punctuation">:</span> 100%/1.6 palatino<span class="token punctuation">,</span> georgia<span class="token punctuation">,</span> serif<span class="token punctuation">;</span>
<span class="token property">padding</span><span class="token punctuation">:</span> 0 1em 2em<span class="token punctuation">;</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token property">border-top-style</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span> 
<span class="token property">font-family</span><span class="token punctuation">:</span> palatino<span class="token punctuation">,</span> georgia<span class="token punctuation">,</span> serif<span class="token punctuation">;</span> 
<span class="token property">font-size</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span> <span class="token property">line-height</span><span class="token punctuation">:</span> 1.6<span class="token punctuation">;</span>
<span class="token property">padding-bottom</span><span class="token punctuation">:</span> 2em<span class="token punctuation">;</span> 
<span class="token property">padding-left</span><span class="token punctuation">:</span> 1em<span class="token punctuation">;</span>
<span class="token property">padding-right</span><span class="token punctuation">:</span> 1em<span class="token punctuation">;</span> 
<span class="token property">padding-top</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
</code></pre></div><h2 id="每个选择器及属性独占一行"><a href="#每个选择器及属性独占一行" class="header-anchor">#</a> 每个选择器及属性独占一行</h2> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">button</span> <span class="token punctuation">{</span> 
    <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
    <span class="token property">background</span><span class="token punctuation">:</span> #00a0e9<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">button</span> <span class="token punctuation">{</span>
     <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span><span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span><span class="token property">background</span><span class="token punctuation">:</span> #00a0e9<span class="token punctuation">;</span> 
 <span class="token punctuation">}</span>
</code></pre></div><h2 id="省略-0-后面的单位"><a href="#省略-0-后面的单位" class="header-anchor">#</a> 省略 0 后面的单位</h2> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">div</span> <span class="token punctuation">{</span>
    <span class="token property">padding-bottom</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">div</span> <span class="token punctuation">{</span>
    <span class="token property">padding-bottom</span><span class="token punctuation">:</span> 0px<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0em<span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre></div><h2 id="避免使用-id-选择器及全局标签选择器防止污染全局样式"><a href="#避免使用-id-选择器及全局标签选择器防止污染全局样式" class="header-anchor">#</a> 避免使用 ID 选择器及全局标签选择器防止污染全局样式</h2> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.header</span> <span class="token punctuation">{</span> 
    <span class="token property">padding-bottom</span><span class="token punctuation">:</span> 0px<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0em<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">#header</span> <span class="token punctuation">{</span> 
    <span class="token property">padding-bottom</span><span class="token punctuation">:</span> 0px<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0em<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
</code></pre></div><h2 id="代码风格"><a href="#代码风格" class="header-anchor">#</a> 代码风格</h2> <h3 id="代码格式化"><a href="#代码格式化" class="header-anchor">#</a> 代码格式化</h3> <ul><li>统一使用展开格式</li></ul> <p>样式书写一般有两种：一种是紧凑格式 (Compact)</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span><span class="token punctuation">{</span> <span class="token property">display</span><span class="token punctuation">:</span> block<span class="token punctuation">;</span><span class="token property">width</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span><span class="token punctuation">}</span>
</code></pre></div><p>一种是展开格式（Expanded）</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span><span class="token punctuation">{</span>
    <span class="token property">display</span><span class="token punctuation">:</span> block<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="代码大小写"><a href="#代码大小写" class="header-anchor">#</a> 代码大小写</h2> <ul><li>样式选择器，属性名，属性值关键字全部使用小写字母书写，属性字符串允许使用大小写。</li></ul> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span><span class="token punctuation">{</span>
	<span class="token property">display</span><span class="token punctuation">:</span>block<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.JDC</span><span class="token punctuation">{</span>
	<span class="token property">DISPLAY</span><span class="token punctuation">:</span>BLOCK<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="选择器-2"><a href="#选择器-2" class="header-anchor">#</a> 选择器</h2> <ul><li>尽量少用通用选择器 *</li> <li>不使用 ID 选择器</li> <li>不使用无具体语义定义的标签选择器</li></ul> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc li</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc li p</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">*</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">#jdc</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc div</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><h2 id="代码缩进"><a href="#代码缩进" class="header-anchor">#</a> 代码缩进</h2> <p>统一使用四个空格进行代码缩进，使得各编辑器表现一致（各编辑器有相关配置）</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="分号"><a href="#分号" class="header-anchor">#</a> 分号</h2> <p>每个属性声明末尾都要加分号；</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="代码易读性"><a href="#代码易读性" class="header-anchor">#</a> 代码易读性</h2> <p>左括号与类名之间一个空格，冒号与属性值之间一个空格</p> <p>逗号分隔的取值，逗号之后一个空格</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">box-shadow</span><span class="token punctuation">:</span> 1px 1px 1px #333<span class="token punctuation">,</span> 2px 2px 2px #ccc<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">box-shadow</span><span class="token punctuation">:</span> 1px 1px 1px #333<span class="token punctuation">,</span>2px 2px 2px #ccc<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>为单个css选择器或新申明开启新行</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc, 
.jdc_logo, 
.jdc_hd</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #ff0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.nav</span><span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc,jdc_logo,.jdc_hd</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #ff0<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token selector">.nav</span><span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>颜色值 rgb() rgba() hsl() hsla() rect() 中不需有空格，且取值不要带有不必要的 0</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span>255<span class="token punctuation">,</span>255<span class="token punctuation">,</span>255<span class="token punctuation">,</span>.5<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span> 255<span class="token punctuation">,</span> 255<span class="token punctuation">,</span> 255<span class="token punctuation">,</span> 0.5 <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>属性值十六进制数值能用简写的尽量用简写</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #ffffff<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>不要为 0 指明单位</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0px 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>属性值引号</p> <p>css属性值需要用到引号时，统一使用单引号</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span> 
	<span class="token property">font-family</span><span class="token punctuation">:</span> <span class="token string">'Hiragino Sans GB'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* 不推荐 */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span> 
	<span class="token property">font-family</span><span class="token punctuation">:</span> <span class="token string">&quot;Hiragino Sans GB&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="属性书写顺序"><a href="#属性书写顺序" class="header-anchor">#</a> 属性书写顺序</h2> <p>建议遵循以下顺序：</p> <ol><li>布局定位属性：display / position / float / clear / visibility / overflow</li> <li>自身属性：width / height / margin / padding / border / background</li> <li>文本属性：color / font / text-decoration / text-align / vertical-align / white- space / break-word</li> <li>其他属性（CSS3）：content / cursor / border-radius / box-shadow / text-shadow / background:linear-gradient …</li></ol> <div class="language-css extra-class"><pre class="language-css"><code><span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">display</span><span class="token punctuation">:</span> block<span class="token punctuation">;</span>
    <span class="token property">position</span><span class="token punctuation">:</span> relative<span class="token punctuation">;</span>
    <span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0 10px<span class="token punctuation">;</span>
    <span class="token property">padding</span><span class="token punctuation">:</span> 20px 0<span class="token punctuation">;</span>
    <span class="token property">font-family</span><span class="token punctuation">:</span> Arial<span class="token punctuation">,</span> <span class="token string">'Helvetica Neue'</span><span class="token punctuation">,</span> Helvetica<span class="token punctuation">,</span> sans-serif<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #333<span class="token punctuation">;</span>
    <span class="token property">background</span><span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span>0<span class="token punctuation">,</span>0<span class="token punctuation">,</span>0<span class="token punctuation">,</span>.5<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token property">-webkit-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-moz-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-o-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-ms-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="css3浏览器私有前缀写法"><a href="#css3浏览器私有前缀写法" class="header-anchor">#</a> CSS3浏览器私有前缀写法</h2> <p>CSS3 浏览器私有前缀在前，标准前缀在后</p> <div class="language-css extra-class"><pre class="language-css"><code><span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token property">-webkit-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-moz-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-o-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">-ms-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h1 id="scss规范"><a href="#scss规范" class="header-anchor">#</a> SCSS规范</h1> <h2 id="scss注释规范"><a href="#scss注释规范" class="header-anchor">#</a> SCSS注释规范</h2> <ul><li>多行注释在使用非 Compressed 模式进行编译后的输出文件中会保留下来，单行注释 // 侧会被移除</li> <li>多行注释和单行注释在 SCSS 编译后输出的压缩 CSS 文件都会被移除</li> <li>当多行注释内容第一个字符是感叹号 “!” 的时候，即 /*! */，SCSS 无论用哪一种编译方式编译注释都会保留</li> <li>注释内容可以加入 SCSS 变量</li></ul> <h2 id="嵌套规范"><a href="#嵌套规范" class="header-anchor">#</a> 嵌套规范</h2> <h3 id="选择器嵌套"><a href="#选择器嵌套" class="header-anchor">#</a> 选择器嵌套</h3> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* CSS */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">body .jdc</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment">/* SCSS */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token selector">body &amp;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* CSS */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc_cover</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc_info</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token selector">.jdc_info_name</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment">/* SCSS */</span>
<span class="token selector">.jdc</span> <span class="token punctuation">{</span>
    <span class="token selector">&amp;_cover</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token selector">&amp;_info</span> <span class="token punctuation">{</span>
        <span class="token selector">&amp;_name</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="属性嵌套"><a href="#属性嵌套" class="header-anchor">#</a> 属性嵌套</h3> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">/* CSS */</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token property">background-repeat</span><span class="token punctuation">:</span> no-repeat<span class="token punctuation">;</span>
    <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/icon.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token property">background-position</span><span class="token punctuation">:</span> 0 0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/* SCSS */</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token selector">background: </span><span class="token punctuation">{</span>
        <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
        <span class="token property">repeat</span><span class="token punctuation">:</span> no-repeat<span class="token punctuation">;</span>
        <span class="token property">image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/icon.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span> 0 0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="变量"><a href="#变量" class="header-anchor">#</a> 变量</h3> <p>可复用属性尽量抽离为页面变量，易于统一维护</p> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// CSS</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token property">border-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token property"><span class="token variable">$color</span></span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> <span class="token variable">$color</span><span class="token punctuation">;</span>
    <span class="token property">border-color</span><span class="token punctuation">:</span> <span class="token variable">$color</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="混合-mixin"><a href="#混合-mixin" class="header-anchor">#</a> 混合(mixin)</h2> <p>根据功能定义模块，然后在需要使用的地方通过 @include 调用，避免编码时重复输入代码段</p> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// CSS</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token property">-webkit-border-radius</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
    <span class="token property">border-radius</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token property">-webkit-border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
    <span class="token property">border-radius</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token keyword">@mixin</span> <span class="token function">radius</span><span class="token punctuation">(</span><span class="token property"><span class="token variable">$radius</span></span><span class="token punctuation">:</span>5px<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token property">-webkit-border-radius</span><span class="token punctuation">:</span> <span class="token variable">$radius</span><span class="token punctuation">;</span>
    <span class="token property">border-radius</span><span class="token punctuation">:</span> <span class="token variable">$radius</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token keyword">@include</span> radius<span class="token punctuation">;</span> <span class="token comment">//参数使用默认值</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token keyword">@include</span> <span class="token function">radius</span><span class="token punctuation">(</span>10px<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token comment">// CSS</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token property">background</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/icon.png<span class="token punctuation">)</span> no-repeat -10px 0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token property">background</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/icon.png<span class="token punctuation">)</span> no-repeat -20px 0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token keyword">@mixin</span> <span class="token function">icon</span><span class="token punctuation">(</span><span class="token property"><span class="token variable">$x</span></span><span class="token punctuation">:</span>0<span class="token punctuation">,</span> <span class="token property"><span class="token variable">$y</span></span><span class="token punctuation">:</span>0<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token property">background</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/icon.png<span class="token punctuation">)</span> no-repeat <span class="token variable">$x</span><span class="token punctuation">,</span> <span class="token variable">$y</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token keyword">@include</span> <span class="token function">icon</span><span class="token punctuation">(</span>-10px<span class="token punctuation">,</span> 0<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token keyword">@include</span> <span class="token function">icon</span><span class="token punctuation">(</span>-20px<span class="token punctuation">,</span> 0<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="占位选择器"><a href="#占位选择器" class="header-anchor">#</a> 占位选择器 %</h2> <p>如果不调用则不会有任何多余的 css 文件，占位选择器以 % 标识定义，通过 @extend 调用</p> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">//scss</span>
<span class="token selector"><span class="token placeholder">%borderbox</span> </span><span class="token punctuation">{</span>
    <span class="token property">-webkit-box-sizing</span><span class="token punctuation">:</span> border-box<span class="token punctuation">;</span>
    <span class="token property">box-sizing</span><span class="token punctuation">:</span> border-box<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token keyword">@extend</span> <span class="token placeholder selector">%borderbox</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="extend-继承"><a href="#extend-继承" class="header-anchor">#</a> extend 继承</h2> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// CSS</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 12px<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 12px<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
    <span class="token property">font-weight</span><span class="token punctuation">:</span> bold<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 12px<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token keyword">@extend</span> .jdc_1<span class="token punctuation">;</span>
    <span class="token property">font-weight</span><span class="token punctuation">:</span> bold<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 或者</span>
<span class="token selector"><span class="token placeholder">%font_red</span> </span><span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 12px<span class="token punctuation">;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span>
    <span class="token keyword">@extend</span> <span class="token placeholder selector">%font_red</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span>
    <span class="token keyword">@extend</span> <span class="token placeholder selector">%font_red</span><span class="token punctuation">;</span>
    <span class="token property">font-weight</span><span class="token punctuation">:</span> bold<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="for-循环"><a href="#for-循环" class="header-anchor">#</a> for 循环</h2> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// CSS</span>
<span class="token selector">.jdc_1 </span><span class="token punctuation">{</span><span class="token property">background-position</span><span class="token punctuation">:</span> 0 -20px<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token selector">.jdc_2 </span><span class="token punctuation">{</span><span class="token property">background-position</span><span class="token punctuation">:</span> 0 -40px<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token selector">.jdc_3 </span><span class="token punctuation">{</span><span class="token property">background-position</span><span class="token punctuation">:</span> 0 -60px<span class="token punctuation">;</span><span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token keyword">@for</span> <span class="token variable">$i</span> <span class="token keyword">from</span> 1 <span class="token keyword">through</span> <span class="token selector">3 </span><span class="token punctuation">{</span>
    <span class="token selector">.jdc_<span class="token variable">#{$i}</span> </span><span class="token punctuation">{</span>
        <span class="token property">background-position</span><span class="token punctuation">:</span> 0 <span class="token punctuation">(</span>-20px<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token variable">$i</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>注意：#{} 是连接符，变量连接使用时需要依赖</p> <h2 id="each-循环"><a href="#each-循环" class="header-anchor">#</a> each 循环</h2> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// CSS</span>
<span class="token selector">.jdc_list </span><span class="token punctuation">{</span>
    <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_list.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_detail </span><span class="token punctuation">{</span>
    <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_detail.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token keyword">@each</span> <span class="token selector"><span class="token variable">$name</span> in list, detail </span><span class="token punctuation">{</span>
    <span class="token selector">.jdc_<span class="token variable">#{$name}</span> </span><span class="token punctuation">{</span>
        <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_<span class="token variable">#{$name}</span>.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token comment">// CSS</span>
<span class="token selector">.jdc_list </span><span class="token punctuation">{</span>
    <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_list.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc_detail </span><span class="token punctuation">{</span>
    <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_detail.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span> blue<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// SCSS</span>
<span class="token keyword">@each</span> <span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$color</span> in <span class="token punctuation">(</span>list<span class="token punctuation">,</span> red<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>detail<span class="token punctuation">,</span> blue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token selector">.jdc_<span class="token variable">#{$name}</span> </span><span class="token punctuation">{</span>
        <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url">url</span><span class="token punctuation">(</span>/img/jdc_<span class="token variable">#{$name}</span>.png<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span> <span class="token variable">$color</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="function-函数"><a href="#function-函数" class="header-anchor">#</a> function 函数</h2> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token keyword">@function</span> <span class="token function">pxToRem</span><span class="token punctuation">(</span><span class="token variable">$px</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">@return</span> <span class="token variable">$px</span> <span class="token operator">/</span> 10px <span class="token operator">*</span> 1rem<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> <span class="token function">pxToRem</span><span class="token punctuation">(</span>12px<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="运算规范"><a href="#运算规范" class="header-anchor">#</a> 运算规范</h2> <p>运算符之间空出一个空格</p> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px <span class="token operator">-</span> 50px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 30px <span class="token operator">/</span> 5<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>注意运算单位，单位同时参与运算，所以 10px 不等于 10，乘除运算时需要特别注意</p> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token comment">// 正确的运算格式</span>
<span class="token selector">.jdc </span><span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px <span class="token operator">-</span> 50px<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px <span class="token operator">+</span> 50px<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px <span class="token operator">*</span> 2<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100px <span class="token operator">/</span> 2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h1 id="less规范"><a href="#less规范" class="header-anchor">#</a> LESS规范</h1> <h2 id="按以下顺序组织"><a href="#按以下顺序组织" class="header-anchor">#</a> 按以下顺序组织</h2> <ol><li>@import;</li> <li>变量声明;</li> <li>样式声明; @import &quot;mixins/</li></ol> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token keyword">@import</span> <span class="token string">&quot;mixins/size.less&quot;</span><span class="token punctuation">;</span>
<span class="token atrule"><span class="token rule">@default-text-color</span><span class="token punctuation">:</span> #333<span class="token punctuation">;</span>
.page</span> <span class="token punctuation">{</span>
  <span class="token property">width</span><span class="token punctuation">:</span> 960px<span class="token punctuation">;</span>
  <span class="token property">margin</span><span class="token punctuation">:</span> 0 auto<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="避免嵌套层级过多"><a href="#避免嵌套层级过多" class="header-anchor">#</a> 避免嵌套层级过多</h2> <ul><li>将嵌套深度限制在 3 级。对于超过 4 级的嵌套，给予重新评估。这可以避免出现过于详实的 CSS 选择器。</li> <li>避免大量的嵌套规则。当可读性受到影响时，将之打断。推荐避免出现多于 20 行的嵌 套规则出现。</li></ul> <div class="language-scss extra-class"><pre class="language-scss"><code><span class="token selector">.main </span><span class="token punctuation">{</span>
  <span class="token selector">.title </span><span class="token punctuation">{</span>
    <span class="token selector">.name </span><span class="token punctuation">{</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-scss extra-class"><pre class="language-scss"><code><span class="token selector">.main-title </span><span class="token punctuation">{</span>
  <span class="token selector">.name </span><span class="token punctuation">{</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #fff<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">10/9/2023, 2:58:10 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#命名" class="sidebar-link reco-side-命名" data-v-cb1513f6>命名</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#选择器" class="sidebar-link reco-side-选择器" data-v-cb1513f6>选择器</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#css-选择器中避免使用标签名" class="sidebar-link reco-side-css-选择器中避免使用标签名" data-v-cb1513f6>css 选择器中避免使用标签名</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#使用直接子选择器" class="sidebar-link reco-side-使用直接子选择器" data-v-cb1513f6>使用直接子选择器</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#尽量使用缩写属性" class="sidebar-link reco-side-尽量使用缩写属性" data-v-cb1513f6>尽量使用缩写属性</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#每个选择器及属性独占一行" class="sidebar-link reco-side-每个选择器及属性独占一行" data-v-cb1513f6>每个选择器及属性独占一行</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#省略-0-后面的单位" class="sidebar-link reco-side-省略-0-后面的单位" data-v-cb1513f6>省略 0 后面的单位</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#避免使用-id-选择器及全局标签选择器防止污染全局样式" class="sidebar-link reco-side-避免使用-id-选择器及全局标签选择器防止污染全局样式" data-v-cb1513f6>避免使用 ID 选择器及全局标签选择器防止污染全局样式</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#代码风格" class="sidebar-link reco-side-代码风格" data-v-cb1513f6>代码风格</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#代码格式化" class="sidebar-link reco-side-代码格式化" data-v-cb1513f6>代码格式化</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#代码大小写" class="sidebar-link reco-side-代码大小写" data-v-cb1513f6>代码大小写</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#选择器-2" class="sidebar-link reco-side-选择器-2" data-v-cb1513f6>选择器</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#代码缩进" class="sidebar-link reco-side-代码缩进" data-v-cb1513f6>代码缩进</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#分号" class="sidebar-link reco-side-分号" data-v-cb1513f6>分号</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#代码易读性" class="sidebar-link reco-side-代码易读性" data-v-cb1513f6>代码易读性</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#属性书写顺序" class="sidebar-link reco-side-属性书写顺序" data-v-cb1513f6>属性书写顺序</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#css3浏览器私有前缀写法" class="sidebar-link reco-side-css3浏览器私有前缀写法" data-v-cb1513f6>CSS3浏览器私有前缀写法</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#scss注释规范" class="sidebar-link reco-side-scss注释规范" data-v-cb1513f6>SCSS注释规范</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#嵌套规范" class="sidebar-link reco-side-嵌套规范" data-v-cb1513f6>嵌套规范</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#选择器嵌套" class="sidebar-link reco-side-选择器嵌套" data-v-cb1513f6>选择器嵌套</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#属性嵌套" class="sidebar-link reco-side-属性嵌套" data-v-cb1513f6>属性嵌套</a></li><li class="level-3" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#变量" class="sidebar-link reco-side-变量" data-v-cb1513f6>变量</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#混合-mixin" class="sidebar-link reco-side-混合-mixin" data-v-cb1513f6>混合(mixin)</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#占位选择器" class="sidebar-link reco-side-占位选择器" data-v-cb1513f6>占位选择器 %</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#extend-继承" class="sidebar-link reco-side-extend-继承" data-v-cb1513f6>extend 继承</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#for-循环" class="sidebar-link reco-side-for-循环" data-v-cb1513f6>for 循环</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#each-循环" class="sidebar-link reco-side-each-循环" data-v-cb1513f6>each 循环</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#function-函数" class="sidebar-link reco-side-function-函数" data-v-cb1513f6>function 函数</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#运算规范" class="sidebar-link reco-side-运算规范" data-v-cb1513f6>运算规范</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#按以下顺序组织" class="sidebar-link reco-side-按以下顺序组织" data-v-cb1513f6>按以下顺序组织</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/standards/css-standards.html#避免嵌套层级过多" class="sidebar-link reco-side-避免嵌套层级过多" data-v-cb1513f6>避免嵌套层级过多</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/sjj-blog/assets/js/app.ab5bad9b.js" defer></script><script src="/sjj-blog/assets/js/3.136f62ef.js" defer></script><script src="/sjj-blog/assets/js/1.a27fba8e.js" defer></script><script src="/sjj-blog/assets/js/15.b0aaec82.js" defer></script>
  </body>
</html>
